package com.smzd.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

public class FileUtil {
	private static File app_root = null;
	public static File getAppRoot(){
    	if(FileUtil.app_root==null){
    		synchronized (FileUtil.class) {
				if(FileUtil.app_root==null){
					FileUtil.app_root = new File(System.getProperty("app.root"));
				}
			}
    	}
    	return app_root;
	}
//
//	public static String getFileEncode(String filePath) {
//		return getFileEncode(new File(filePath));
//	}
//
//	public static String getFileEncode(File file) {
//		return getFileCharset(file).name();
//	}
//
//	public static Charset getFileCharset(File file) {
//		Charset defaultFileCharset = Charset.forName("UTF-8");
//		CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
//		detector.add(JChardetFacade.getInstance());
//		Charset charset = null;
//		try {
//			charset = detector.detectCodepage(file.toURI().toURL());
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return charset == null ? defaultFileCharset : charset;
//	}

	public static void copyFolder(File srcFolder, File tarFolder) {
		if (!srcFolder.exists() || !srcFolder.isDirectory()) {
			return;
		}
		if (!tarFolder.exists()) {
			tarFolder.mkdirs();
		}
		File[] childFiles = srcFolder.listFiles();
		if (childFiles != null) {
			File tempFile = null;
			for (int i = 0, len = childFiles.length; i < len; i++) {
				tempFile = childFiles[i];
				if (tempFile.isDirectory()) {
					File tarFile = new File(tarFolder, tempFile.getName());
					copyFolder(tempFile, tarFile);
				} else if (tempFile.isFile()) {
					File tarFile = new File(tarFolder, tempFile.getName());
					copyFile(tempFile, tarFile);
				}
			}
		}
	}

	public static void copyFile(File sourceFile, File targetFile) {
		if (!sourceFile.exists()) {
			return;
		}
		if (!sourceFile.isFile()) {
			return;
		}
		try {
			FileInputStream input = new FileInputStream(sourceFile);
			downloadFile(input, targetFile);
			input.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public static boolean deleteFile(File file) {
		return file.delete();
	}

	public static boolean deleteFolder(File dir) {
		if (!dir.exists()) {
			return true;
		}
		if (dir.isDirectory()) {
			String[] files = dir.list();
			for (int i = 0; i < files.length; i++) {
				boolean success = deleteFolder(new File(dir, files[i]));
				if (!success) {
					return false;
				}
			}
		}
		return dir.delete();
	}

	public static void saveContent(String content, File file) {
		saveContent(content, file, false);
	}

	private static void saveContent(String content, File file, boolean append) {
		saveContent(content, file, append, "UTF-8");
	}

	public static void saveContent(String content, File file, boolean append, String charset) {
		try {
			OutputStream out = new FileOutputStream(file, append);
			Writer writer = new OutputStreamWriter(out, charset);
			writer.write(content);
			writer.close();
			out.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static StringBuffer readContent(File file) {
		return readContent(file, "\r\n");
	}
	
	public static StringBuffer readContent(File file, String carryReturn) {
		return readContent(file, carryReturn, "UTF-8");
	}

	public static StringBuffer readContent(File file, String carrayReturn, String charset) {
		InputStream in;
		try {
			in = new FileInputStream(file);
			return readContent(in, carrayReturn, charset);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static StringBuffer readContent(InputStream in) {
		return readContent(in, "\r\n", "UTF-8");
	}
	public static StringBuffer readContent(InputStream in, String carryReturn) {
		return readContent(in, carryReturn, "UTF-8");
	}

	public static StringBuffer readContent(InputStream in, String carryReturn, String charset) {
		try {
			Reader reader = new InputStreamReader(in, charset);
			BufferedReader r = new BufferedReader(reader);
			String line = r.readLine();
			if (line == null) {
				return null;
			}
			StringBuffer sb = new StringBuffer(line);
			if (carryReturn != null && carryReturn.length() > 0) {
				while ((line = r.readLine()) != null) {
					sb.append(carryReturn).append(line);
				}
			} else {
				while ((line = r.readLine()) != null) {
					sb.append(line);
				}
			}
			return sb;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public static void downloadFile(InputStream is, File file) throws IOException {
		FileOutputStream output = new FileOutputStream(file);
		byte[] b = new byte[1024 * 4];
		int len;
		while ((len = is.read(b)) != -1) {
			output.write(b, 0, len);
		}
		output.flush();
		output.close();
	}
}
